package com.zntg.pdf;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class Test { 
    public static void main(String[] args) throws Exception {
        createPdf();
         System.out.println("success");
     }
         
     public static void createPdf() throws IOException, DocumentException {
          // pdf模板
         String fileName = "D:\\work\\ylcaifu\\智能投顾二期\\报表平台\\pdf生成好的不带logo的模板\\优策月月盈3号份额确认书-（追加申购模板）2.pdf";
         PdfReader reader = new PdfReader(fileName);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         /* 将要生成的目标PDF文件名称 */
         PdfStamper ps = new PdfStamper(reader, bos);
         PdfContentByte under = ps.getUnderContent(1);

         /* 使用中文字体 */
         BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
         ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
         fontList.add(bf);

         /* 取出报表模板中的所有字段 */
         AcroFields fields = ps.getAcroFields();

         fields.setSubstitutionFonts(fontList);
         fillData(fields, data());

         /* 必须要调用这个，否则文档不会生成的 */
         ps.setFormFlattening(true);
         ps.close();

         //生成pdf路径
         OutputStream fos = new FileOutputStream("itext.pdf");
         fos.write(bos.toByteArray());
         fos.flush();
         fos.close();
         bos.close();

         }

     /**
      * 填充模板
      * */
     public static void fillData(AcroFields fields, Map<String, String> data)
                 throws IOException, DocumentException {
                 for (String key : data.keySet()) {
                 String value = data.get(key);
                     if (!key.startsWith("Image")){
                         //设置文本域参数值
                         fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的
                     }else{
                         //将图片写入指定的field
                      /*   Image image = Image.getInstance("D:\\work\\learn\\itext-master\\seal.png");
                         //将图片写入指定的field
                         PushbuttonField pb = fields.getNewPushbuttonFromField(key);
                         pb.setImage(image);
                         fields.replacePushbuttonField(key, pb.getField());*/

                     }
                 }
         }

                 /**
          * 填充数据源
          * 其中data存放的key值与pdf模板中的文本域值相对应
          * */
     public static Map<String, String> data() {
         Map<String, String> data = new HashMap<String, String>();
         data.put("userName", "令狐冲");
         data.put("bianhao", "002");
         data.put("year", "2018");
         data.put("month", "12");
         data.put("day", "05");
         data.put("MONEY", "一千万");
         data.put("money", "10000000");
         data.put("fene", "1000000");
         data.put("shengoufei", "10000");
         data.put("yearBottom", "2018");
         data.put("monthBottom", "12");
         data.put("dayBottom", "05");
         data.put("Image", "D:\\work\\learn\\itext-master\\seal.png");
         return data;
         }




}

